package com.san.InterfManage.controller;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.InterfManage.domain.InterfManageDo;
import com.san.InterfManage.service.InterfManageService;
import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
/**
 * 接口管理
 * @author Zhangye_hz
 * @date 2020年3月16日
 */
@RequestMapping("/interf")
@Controller
public class InterfManageController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(InterfManageController.class);
	@Autowired
	InterfManageService interfManageService;
	/**
	 * 本功能地址前缀
	 */
	private static final String PREFIX = "interface/management";
	
	@RequiresPermissions("interf:view")
	@GetMapping("")
	String management(Model model) {
		return PREFIX+"/view";
		
	}
	/**
	 * 添加接口页面
	 * @param model
	 * @return
	 */
	@GetMapping("/add")
	@RequiresPermissions("interf:add")
	String add(Model model) {
		return PREFIX + "/add";
	}
	/**
	 * 添加接口
	 * @param interfManageDO
	 * @return
	 */
	@RequiresPermissions("interf:add")
	@Log("添加接口")
	@PostMapping("/save")
	@ResponseBody
	R save(InterfManageDo interfManageDo) {
		try {
			if (interfManageService.add(interfManageDo) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("保存接口出错", e);
			return R.error();
		}
		return R.error();
	}
	/**
	 * 接口管理列表
	 * 
	 * @param params
	 * @return
	 */
	@GetMapping("/list")
	@ResponseBody
	public PageUtils findListSource(@RequestParam Map<String, Object> params) {
		PageUtils pageUtils = null;
		try {
			Query query = new Query(params);
			pageUtils = interfManageService.findListSource(query);	
		} catch(Exception e) {
			logger.error("分页获取接口列表出错", e);
		}
		return pageUtils;
	}
	@RequiresPermissions("interf:remove")
	@Log("删除接口")
	@PostMapping("/remove")
	@ResponseBody
	R remove(Long id) {
		try {
			if (interfManageService.remove(id) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("删除接口出错", e);
		}
		return R.error();
	}
	/**
	 * 控制接口状态
	 * @param manageId
	 * @param interfState
	 * @return
	 */
	@PostMapping(value = "/changeStatus")
	@RequiresPermissions("interf:changeStatus")
	@ResponseBody
	public R changeStatus(Long manageId,Long interfState ) {
		String label = "";
		if (interfState == 1) {
			label = "禁用";
		} else {
			label = "启用";
		}
		InterfManageDo interfManageDo = new InterfManageDo();
		interfManageDo.setManageId(manageId);
		interfManageDo.setInterfState(interfState);
		try {
			interfManageService.changeStatus(interfManageDo);
			return R.ok("接口" + label + "成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok("接口" + label + "失败");
	}
	/**
	 * 编辑接口信息
	 * 
	 * @param model
	 * @param tableId 表ID
	 * @return
	 */
	@Log("编辑接口信息")
	@RequiresPermissions("interf:edit")
	@GetMapping("/edit/{manageId}")
	String edit(Model model, @PathVariable("manageId") Long manageId) {
		InterfManageDo interfManageDo = null;
		try {
			interfManageDo =  (InterfManageDo) interfManageService.getById(manageId);
		} catch (Exception e) {
			logger.error("获取接口信息出错", e);
		}
		model.addAttribute("interfManage", interfManageDo);
		return PREFIX + "/edit";
	}
	/**
	 * 更新接口信息
	 * 
	 * @param model
	 * @param tableId 表ID
	 * @return
	 */
	@RequiresPermissions("interf:edit")
	@Log("更新接口信息")
	@PostMapping("/doEdit")
	@ResponseBody
	R doEdit(InterfManageDo interfManageDo ) {
		try {
			if(interfManageService.update(interfManageDo) >0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("更新接口信息出错",e);
			return R.error();
		}
		return R.error();
	}
}
